package com.framework.loippi.api.utils;

import java.math.BigDecimal;

public class JuliUtils {
private static double EARTH_RADIUS = 6378.137; 
    
    private static double rad(double d) { 
        return d * Math.PI / 180.0; 
    }
    
    public static Double getDistance(String lat1Str, String lng1Str, Double lat2Str, Double lng2Str) {
        Double lat1 = Double.parseDouble(lat1Str);
        Double lng1 = Double.parseDouble(lng1Str);
//        Double lat2 = Double.parseDouble(lat2Str);
//        Double lng2 = Double.parseDouble(lng2Str);
        
        Double lat2 = lat2Str;
        Double lng2 = lng2Str;
          
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double difference = radLat1 - radLat2;
        double mdifference = rad(lng1) - rad(lng2);
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(mdifference / 2), 2)));
        distance = distance * EARTH_RADIUS;
        distance = Math.round(distance * 10000) / 10000;
        BigDecimal distanceStr = new BigDecimal(distance);   
        double df = distanceStr.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 
          
        return df;
    }
}
